﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MobileAPI.Web.Data;
using MobileAPI.BLL;
using MobileAPI.Entity.Model;
using MobileAPI.Common;

namespace MobileAPI.Web.APIs
{
    [MobileAPI(Code = "MC0005", Name = "新增报修")]
    public class AddNewRepairs : IProcessMessage
    {
        private BLLRepairsInfo bll;
        public BLLRepairsInfo Bll
        {
            get
            {
                if (bll == null)
                    bll = BLLFactory.getInstance().CreateRepairsInfoBLL();
                return bll;
            }
        }


        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);
                // 获取参数字段（具体请参见文档）

                string UserName = reqMsg.Body.GetFieldValue("UserName");
                ERepairsInfo repair = new ERepairsInfo();
                repair.CustomerArea = reqMsg.Body.GetFieldValue("CustomerArea");
                repair.CustomerName = reqMsg.Body.GetFieldValue("CustomerName");
                repair.RoomName = reqMsg.Body.GetFieldValue("RoomName");
                repair.Instrument = reqMsg.Body.GetFieldValue("Instrument");
                repair.InstrumentID = reqMsg.Body.GetFieldValue("InstrumentID");
                repair.RepairsDate = reqMsg.Body.GetFieldValue("RepairsDate");
                repair.RepairsStaff = reqMsg.Body.GetFieldValue("RepairsStaff");
                repair.RepairsPhone = reqMsg.Body.GetFieldValue("RepairsPhone");
                repair.Priority = reqMsg.Body.GetFieldValue("Priority");
                repair.IsStop = reqMsg.Body.GetFieldValue("IsStop");
                repair.Diagnosis = reqMsg.Body.GetFieldValue("Diagnosis");

                if (string.IsNullOrEmpty(UserName))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }

                bool IsSuccess= Bll.InsertRepairs(repair,UserName);
                if (IsSuccess)
                {
                    rspMsg.Head.RspCode = MessageCode.Success.Code;
                    rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                    return rspMsg;
                }
                else
                {
                    rspMsg.Head.RspCode = MessageCode.Server_Error.Code;
                    rspMsg.Head.RspMsg = MessageCode.Server_Error.Msg;

                    return rspMsg;
                }
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("Login-" + e);
                throw e;
            }

        }
        #endregion
    }
}